package com.yunquan.ohana.ui.calendar.task.filter

import android.os.Bundle
import androidx.core.graphics.toColorInt
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.RoleEntity
import com.yunquan.ohana.ui.calendar.profiles.add.AddProfilesActivity

class TaskFilterRoleItemViewModel(
    viewModel: TaskFilterViewModel,
    val data: RoleEntity,
    edit: Boolean,
    check: Boolean,
) : ItemViewModel<TaskFilterViewModel>(viewModel) {
    val prefix = ObservableField(data.name?.first().toString())
    val name = ObservableField<String>(data.name)
    val account = ObservableField<String>(data.account)
    val backColor = ObservableField(data.getColorData().inner.toColorInt())
    val isEdit = ObservableField(edit)
    val checked = ObservableField(check)
    val checkIcon = ObservableField(if (check) R.mipmap.ic_on else R.mipmap.ic_off)

    val onCheckChange = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val b = checked.get()!!
            checked.set(!b)
            if (b) {
                checkIcon.set(R.mipmap.ic_off)
            } else {
                checkIcon.set(R.mipmap.ic_on)
            }
        }
    })

    val onEditClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("deviceId", viewModel.mDeviceId)
            bundle.putBoolean("userType", viewModel.mUserType)
            bundle.putString("data", Gson().toJson(data))
            viewModel.startActivityResult(AddProfilesActivity::class.java,bundle)
        }
    })
}